package com.ljh.myaiagent.advisor;

import org.springframework.ai.chat.client.advisor.api.*;
import org.springframework.ai.chat.prompt.Prompt;
import reactor.core.publisher.Flux;

import java.util.HashMap;

/**
 * 自定义Re2 Advisor
 * 提高大模型的推理能力
 */
public class ReReadingAdvisor implements CallAroundAdvisor, StreamAroundAdvisor {

    @Override
    public AdvisedResponse aroundCall(AdvisedRequest advisedRequest, CallAroundAdvisorChain chain) {
        return chain.nextAroundCall(this.before(advisedRequest));
    }

    @Override
    public Flux<AdvisedResponse> aroundStream(AdvisedRequest advisedRequest, StreamAroundAdvisorChain chain) {
        return chain.nextAroundStream(this.before(advisedRequest));
    }

    private AdvisedRequest before(AdvisedRequest request){
        HashMap<String, Object> advisorUserHashMap = new HashMap<>(request.userParams());
        advisorUserHashMap.put("re2_input_query", request.userText());
        // 修改用户提示词
        String newUserText = """
                %s
                Read the question again: %s
                """.formatted(request.userText(), request.userText());
        return AdvisedRequest.from(request).userText(newUserText)
                .userParams(advisorUserHashMap)
                .build();
    }
    @Override
    public String getName() {
        return this.getClass().getSimpleName();
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
